How to Use INDEX MATCH with Excel VBA

您所在的位置:网站首页 match vba How to Use INDEX MATCH with Excel VBA

How to Use INDEX MATCH with Excel VBA

2023-03-14 23:12| 来源: 网络整理| 查看: 265

You can utilize the built-in Excel Worksheet functions such as the VLOOKUP Function, the CHOOSE Function, and the PMT Function in your VBA code and applications as well. In fact, most of the Excel worksheet functions can be accessed and used in VBA code. Like, use INDEX & MATCH Worksheet Functions in Excel VBA code.

Why would you want to use Excel Worksheet functions in your VBA code? Well to extend the functionality of the code you are using. Also, you don’t have to come up with your own functions, unless you really need to, if the functionality is already there. All you basically need to do is access the function you need since it’s already there and there is then no need to reinvent the wheel.

So, let’s get started with an example showing how to use INDEX  MATCH with Excel VBA.

We are going to utilize the INDEX and MATCH Functions in Excel VBA code, in order to create a simple UserForm. Using the form, the user selects the name of the student, and then the corresponding gender of said student and eye color is retrieved and returned.

Table of Contents hide Download Practice Workbook 9 Easy Steps to Use INDEX and MATCH Within VBA Code Step1: Apply INDEX and MATCH Functions in Dataset Step 2: Change the Name of B Column into StudentNames Step 3: Open Visual Basic Window Step 4: Change Properties and Add Text Boxes Step 5: Add a Button from Toolbox Step 6: Insert VBA Code Step 7: Insert a Command Button Step 8: View Lookup Code Step 9: Exhibit Final Result Conclusion Review Section: Test Your Understanding Related Articles Download Practice Workbook

Please download the practice workbook to practice yourself.

INDEX-MATCH.xlsx 9 Easy Steps to Use INDEX and MATCH Within VBA Code

The INDEX and MATCH Functions are often used in combination in formulas, in order to perform advanced lookups. The two in combination offer certain advantages over VLOOKUP.

We have already covered in detail, how to use INDEX and MATCH to perform advanced lookups in an Excel workbook as a straight worksheet formula, in a previous tutorial. We are now going to see how to use the INDEX and MATCH Functions together in VBA code, in order to confer similar functionality to the look up UserForm we are going to create.

AD Step1: Apply INDEX and MATCH Functions in Dataset We are starting off with two sheets in our macro-enabled workbook. One is an empty sheet called UserForm, the other is a sheet called Student Information, which contains a range showing student names, their corresponding gender, and eye color as shown below.

Dataset to use Index and match function in excel vba

AD

Let’s remind ourselves quickly if we wanted to use the INDEX and MATCH Functions in one formula, in the actual worksheet to give us the gender of the name of the student we want to look up. We would use the following formula:

=INDEX(B2:B31, MATCH("Diana Graham", A2:A31, 0))

use Index and match function in excel vba

AD Upon pressing CTRL-ENTER, we get the value of Females returned, as the gender as shown below.

Index and match function in excel vba

Read More: How to Use INDEX MATCH Formula in Excel (9 Examples)

AD Step 2: Change the Name of B Column into StudentNames We will now name the range A2: A31, StudentNames as shown below.

 Index and match function in excel vba

AD Hide the student information sheet, by right-clicking and selecting Hide. It’s a good idea to superficially hide the back-end worksheets that contain the information, that you don’t want the user to edit or see.

Read More: INDEX-MATCH with Multiple Matches in Excel (6 Examples)

Step 3: Open Visual Basic Window Now with the UserForm sheet activated, we go to Developer > Code > Visual Basic in order to open the Visual Basic Editor (VBE). Once in the VBE interface, we go to Insert, UserForm as shown below.

Read More: How to Create Excel VBA UserForm (with Detailed Steps)

AD Step 4: Change Properties and Add Text Boxes Using the Properties Window, we will rename our form to StudentLookup, change the Caption to Lookup Student Information, change the BackColor to light blue, and set the height to 300 px and the width to 350 px. If the Properties Window is not showing up, press the F4 key on your keyboard in order to see it.

Textbox to use Index and match function in excel vba

AD We will now insert a label using the Toolbox (if you cannot see the Toolbox, for some reason go to View, Toolbox), change the Caption to Choose a student and we will change the BackColor to white in this case. We will set the font to Georgia, the font style to bold, the font size to 12, and the center align the text. The special effect used will be the 1– fmSpecialEffectRaised as shown below.

Textbox to use Index and match function in excel vba

AD Now we will insert a combo box below the label. Name this combo box cmdStudentName and for the RowSource, type StudentNames.

Textbox to use Index and match function in excel vba

AD In order to see the effect of setting the RowSource of the combo box, Click the Run Sub/UserForm button. Now because of setting the RowSource to the named range, when the user clicks on the drop-down arrow on the UserForm, the combo box shows the student names from the named range, automatically as shown below.

Textbox to use Index and match function in excel vba

AD Close the UserForm by clicking on the close button. Press Alt-F11 in order to go back to the VBE. Once back in the VBE, add another label to the UserForm (below the combo box) and change the Caption to Gender and we will change the BackColor to white in this case. We will set the font to Georgia, the font style to bold, the font size to 12, and the center align the text. The special effect used will be the 1– fmSpecialEffectRaised as shown below.

Textbox to use Index and match function in excel vba

AD Create a textbox below the Gender label, and name it extender. Add another label called Eye Colour and a textbox named txtEyeColour as shown below. Use the same properties for the label as for the two other labels previously added to the form, in order to ensure that the UserForm has a consistent look.

Textbox to use Index and match function in excel vba

AD Now Select all the controls, added to the UserForm, thus far using the control key.

AD Center horizontally, as shown below.

Alignment of checkboxes to use Index and match function in excel vba

AD

Read More: How to Use INDEX MATCH with Multiple Criteria in Excel (3 Ways)

Similar Readings

Excel INDEX MATCH If Cell Contains Text How to Use INDEX MATCH Instead of VLOOKUP in Excel Examples with INDEX-MATCH Formula in Excel (8 Approaches) 22 Macro Examples in Excel VBA Step 5: Add a Button from Toolbox Next, Add a button to the form using the Toolbox. Change the Name of the button to cmdLookUp, the BackColor to light orange, keep the Tahoma font, and change the style to bold, finally change the Caption of the button to Look up Student Details as shown below.

Read More: INDEX-MATCH Formula to Find Minimum Value in Excel (4 Ways)

AD Step 6: Insert VBA Code Right-click, the newly added button, and select View Code.

VBA code to Index and match function in excel vba

AD Enter the following code for the button click event:

Raw Code in Excel VBA

AD Dim a As Variant Dim b As Variant Dim c As Variant a = cmdStudentName.Value Sheets("StudentInformation").Activate If a = "" Then b = "" Let txtGender.Text = b c = "" Let txtEyeColour.Text = c Else b = Application.WorksheetFunction.Index(Sheets("StudentInformation").Range("B2:B31"), Application.WorksheetFunction.Match(a, Sheets("StudentInformation").Range("A2:A31"), 0)) c = Application.WorksheetFunction.Index(Sheets("StudentInformation").Range("C2:C31"), Application.WorksheetFunction.Match(a, Sheets("StudentInformation").Range("A2:A31"), 0)) Let txtEyeColour.Text = c End If

We start off by declaring three variables and assigning the variant data type to these declared variant data types. The variant data type is a good data type to start with. Because when working with worksheet functions, you may not always be sure of the outputs. Therefore use the variant data type, when you are starting out.

AD

Later on, it is advisable to use one of the other more specific data types such as integer or string. For more advanced longer code, the variant data type does not use memory as efficiently as the other data types.

Variable a draw the value from the option the user selects in the drop-down combo box on the UserForm. If there is no selection, then all the other textboxes are empty.

If you select a student name from the combo box on the UserForm, then variable b draws value by using the INDEX Worksheet Function in combination with the MATCH Function in the VBA code, as shown.

AD

It looks up the value using basically the same syntax as the worksheet function.  When using worksheet functions in VBA, the VBA IntelliSense in this particular case is not very intuitive, therefore familiarity with the syntax gleaned from worksheet knowledge is recommended.  Variable c draws value by using the INDEX Worksheet Function in combination with the MATCH Function in the VBA code when the user selects an option from the combo box.

Variable b attains value from the gender column in the worksheet, whereas variable c gets value from the Eye colour column in the worksheet.

The gender textbox is populated with b’s value and the eye color textbox is populated with c’s value.

Read More: Excel VBA Events (A Complete Guideline)

AD Step 7: Insert a Command Button Now go to the worksheet called UserForm in your workbook. Format it, as shown below, and insert the image provided by ExcelDemy.

Student Information Image 1

AD Go to Developer > Controls > Insert > ActiveX Controls.

Student Information Image 2

AD Insert a button as shown.

Student Information Image 3

AD With the button selected, go to Developer > Controls > Properties.

Student Information Image 4

AD Change the Name of the button to cmdShowForm and the Caption to Lookup Student Information.

Properties to use Index and match function in excel vba

Read More: How to Create a UserForm: an Overview

AD Step 8: View Lookup Code Right-click the button and select View Code as shown below.

View code to Index and match function in excel vba

AD Enter the following code: Private Sub cmdShowForm_Click() StudentLookup.Show End Sub

Index and match function in excel vba

Read More: How to Use Excel VBA Userform (2 Suitable Examples)

Similar Readings

INDEX MATCH vs VLOOKUP Function (9 Practical Examples) Difference Between Subroutine & Function in Excel VBA How to Use VBA User Defined Function (4 Suitable Examples) List of 10 Mostly Used Excel VBA Objects (Attributes & Examples)

AD Step 9: Exhibit Final Result Return to the worksheet. Make sure the unchecked Design Mode.Index and match function in excel vba Click the button in order to show the form.

Index and match function in excel vba

AD Select a student’s name using the combo box. The code will return the student’s gender and eye color automatically.

Index and match function in excel vba

Remember to save your workbook as a macro-enabled workbook, if you haven’t done so already and there you have it, we use INDEX & MATCH Worksheet Functions in Excel VBA code in order to create a lookup form.

AD

Read More: Excel INDEX-MATCH Formula to Return Multiple Values Horizontally

Conclusion

Excel has many useful worksheet functions, which can be utilized in VBA, as like as, using INDEX & MATCH Worksheet Functions in Excel VBA code. These functions will allow you to extend your VBA code. If you already know how they work in a standard Excel worksheet then the learning curve is not that great by adapting the knowledge for VBA. Accessing the worksheet functions, in one’s VBA code can be a real time-save. Because one does not have to develop custom functions for functionality that is already there.

AD

Please feel free to comment and tell us if you use worksheet functions in your VBA code and applications.

Review Section: Test Your Understanding

1) Setup a simple list in column A of three items namely tangerines, carrots, and oranges, then in the cell next to each item in column B list whether the items in column A are fruits or vegetables, once you’ve completed setting up your sample data, use the INDEX & MATCH combination function to deliver whether carrots are fruits or vegetables.

2) Use this data set from ESPN on the NFL head coaches and the respective team they are coaching. Create a user form that allows a user to input the name of a certain coach in a textbox. Then have the team he is coaching delivered in another textbox when the user clicks submit. Use INDEX & MATCH worksheet function combination within your VBA code.

AD Related Articles INDEX-MATCH with Duplicate Values in Excel (3 Quick Methods) Introduction to VBA Features and Applications Using Macro Recorder in Excel (With Easy Steps) How to Use VBA Modules in Excel (8 Simple Ways) What Are Excel Function Arguments (A Detailed Discussion) How to Use IF with INDEX & MATCH Functions in Excel (3 Ways) INDEX MATCH for Multiple Criteria in Rows and Columns in Excel SaveSavedRemoved 0 How to Use INDEX MATCH with Excel VBA PreviousHow to Tally Survey Results in Excel (Step by Step) How to Use INDEX MATCH with Excel VBA NextHow to Use Excel Formula to Calculate Percentage of Grand Total

Tags: Index Match ExcelVBA in Excel

Pabok

We will be happy to hear your thoughts Leave a reply Cancel reply


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3